﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.Member
{
    public class SymptomRecordes : PatientBasePage
    {
        protected List<DiseaseSymptomInfo> PrimarySymptomList { get; set; }
        protected List<DiseaseSymptomInfo> SecondSymptomList { get; set; }
        protected string[] SymptomDegree { get; set; }
        protected string RecordDate {get; set;}
        protected SymptomRecordesInfo SymptomRecordInfo { get; set; }

        private Dictionary<int, int> _SymptomRecordData;
        //protected string 

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            PrimarySymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(PRInfo.DiseaseId, Constants.PRIMARY_SYMPTOM);
            SecondSymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(PRInfo.DiseaseId, Constants.SECOND_SYMPTOM);
            

        }
        protected void HandleGet()
        {
            SymptomDegree = Constants.SYMPTOM_DEGREE;
            int id = RequestTools.GetQueryInt("id", 0);
            if (id > 0) SymptomRecordInfo = PatientBLL.Instance.GetSymptomRecordes(id, PRInfo.Id);
            if (SymptomRecordInfo == null) SymptomRecordInfo = new SymptomRecordesInfo();
            if (SymptomRecordInfo.Id <= 0)
            {
                RecordDate = "";
            }
            else
            {
                RecordDate = SymptomRecordInfo.RecordDate.ToString("yyyy-MM-dd");
                this.SetSymptomRecordData(SymptomRecordInfo.SymptomInfo);
            }
        }

        protected void HandlePost()
        {
            SymptomRecordesInfo info = new SymptomRecordesInfo();
            info.Id = RequestTools.GetFormInt("symrecordid",-1);
            bool isCreated = true;
            if (info.Id <= 0) isCreated = false;
            info.PatientRecordId = PRInfo.Id;
            StringBuilder sb = new StringBuilder();
            foreach (DiseaseSymptomInfo syminfo in PrimarySymptomList)
            {
                sb.AppendFormat("@{0}={1}", syminfo.SymptomId, RequestTools.GetFormString("sym" + syminfo.SymptomId));
            }
            foreach (DiseaseSymptomInfo syminfo in SecondSymptomList)
            {
                sb.AppendFormat("@{0}={1}", syminfo.SymptomId, RequestTools.GetFormString("sym" + syminfo.SymptomId));
            }
            info.SymptomInfo = sb.ToString();
            info.Remark = RequestTools.GetFormString("remark");
            info.RecordDate = Convert.ToDateTime(RequestTools.GetFormString("recorddate"));
            info.UpdateDate = DateTime.Now;
            if (isCreated)
            {
                PatientBLL.Instance.UpdateSymptomRecordes(info);
                this.AddMsgLine("更新症状记录成功");
                this.SetUrl("ListSymptomRecordes.aspx");
            }
            else
            {
                PatientBLL.Instance.AddSymptomRecordes(info);
                this.AddMsgLine("添加症状记录成功");
                this.SetUrl("ListSymptomRecordes.aspx");
            }
            
        }

        private void SetSymptomRecordData(string datastr)
        {
            if (_SymptomRecordData == null) _SymptomRecordData = new Dictionary<int, int>();
            _SymptomRecordData.Clear();
            string[] spliter = new string[] { "@" };
            string[] spliter2 = new string[] { "=" };
            string[] datapair = datastr.Split(spliter,StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in datapair)
            {
                string[] strArray = str.Split(spliter2, StringSplitOptions.RemoveEmptyEntries);
                int key = Convert.ToInt32(strArray[0]);
                int val = Convert.ToInt32(strArray[1]);
                _SymptomRecordData.Add(key, val);
            }
        }
        protected int GetSymptomRecordData(int key)
        {
            if (_SymptomRecordData == null) return Constants.SYMPTOM_NONE;
            if (_SymptomRecordData.ContainsKey(key) == false) return Constants.SYMPTOM_NONE;
            int data = _SymptomRecordData[key];
            if (data > Constants.SYMPTOM_NONE) data = Constants.SYMPTOM_NONE;
            return data;
        }
    }
}
